<?php
//代理商列表
namespace app\common\logic;

use think\Db;
use app\common\logic\Check;

class Agentlogic
{
    /**
     *添加代理商
     * @param $data
     * @return array
     */
    public function add($data)
    {
        $returnData = $this->checkAgent($data);
        if ($returnData['code'] != 0) {
            return $returnData;
        }
        $insertData = $returnData['data'];
        $insertData['type'] = 1;
        $insertData['addtime'] = time();
        $insertData['oid'] = 1;

        //判断用户是否存在
        $check = Db::name('Agent')->where(['phone'=>$insertData['phone'],'type'=>1])->find();
        if($check){
            return output('-1', '代理商号码已经存在');
        }
        if(Db::name( 'Agent')->insert($insertData)){
            $sid = Db::name('Agent')->getLastInsID();
            $cdate['sid'] = $sid;
            return output(0, '添加成功');
        }else{
            return output(-1, '添加失败');
        }
    }

    /**
     *添加地推
     * @param $data
     * @return array
     */
    public function addEmployee($data, $isHome = false)
    {
        $returnData = $this->checkAgent($data, true);
        if ($returnData['code'] != 0) {
            return $returnData;
        }
        $insertData = $returnData['data'];
        $insertData['type'] = 2;
        $insertData['addtime'] = time();
        $insertData['oid'] = 1;

        //判断归属代理商
        if (!isset($data['belong_agent']) && empty($data['belong_agent'])) {
            return output(-1, '请选择归属代理商');
        }
        $data['belong_agent'] = intval($data['belong_agent']);
        $agentInfo = Db::name('agent')->field('id')->where(['id' => $data['belong_agent']])->find();
        if (empty($agentInfo)) {
            return output(-1, '请选择归属代理商');
        }
        $insertData['belong_agent'] = $data['belong_agent'];

        $check = Db::name('agent')->where(['phone'=>$insertData['phone'],'type'=>2])->find();
        if($check){
            return output('-1', '地推号码已经存在');
        }

        $sid = Db::name('agent')->insert($insertData);
        if($sid){

            return output(0, '添加成功');
        }else{
            return output(-1, '添加失败');
        }




    }

    /**
     * 编辑代理商
     * @param array $data
     * @return array
     */
    public function editEmployee($data = [])
    {
        $id = intval($data['id']);
        if (empty($id)) {
            return output(-1, '参数错误');
        }
        $returnData = $this->checkAgent($data, true);
        if ($returnData['code'] != 0) {
            return $returnData;
        }
        $insertData = $returnData['data'];
        $insertData['type'] = 2;
        $insertData['update_time'] = time();
        $insertData['oid'] = 1;

        //判断代理商
        if (!isset($data['belong_agent']) && empty($data['belong_agent'])) {
            return output(-1, '请选择归属代理商');
        }
        $data['belong_agent'] = intval($data['belong_agent']);
        $agentInfo = Db::name('agent')->field('id')->where(['id' => $data['belong_agent']])->find();
        if (empty($agentInfo)) {
            return output(-1, '请选择归属代理商');
        }
        $insertData['belong_agent'] = $data['belong_agent'];

        $check = Db::name('agent')->where('id','<>',$id)->where(['phone'=>$insertData['phone'],'type'=>2])->find();
        if ($check) {
            return output(-1, '地推号码已经存在');
        }
        $res = Db::name('agent')->where(['id' => $id])->update($insertData);
        if ($res) {

            return output(0, '修改成功');
        } else {
            return output(-1, '修改失败');
        }

    }

    /**
     *代理商编辑
     * @param array $data
     * @return array
     */
    public function edit($data = [])
    {
        $id = intval($data['id']);
        if (empty($id)) {
            return output(-1, '参数错误');
        }
        $returnData = $this->checkAgent($data);
        if ($returnData['code'] != 0) {
            return $returnData;
        }
        $insertData = $returnData['data'];
        $insertData['type'] = 1;
        $insertData['update_time'] = time();
        $insertData['oid'] = 1;
        $check = Db::name('agent')->where('id','<>',$id)->where(['phone'=>$insertData['phone'],'type'=>1])->find();
        if ($check) {
            return output(-1, '代理商号码已经存在');
        }
        $res = Db::name('agent')->where(['id' => $id])->update($insertData);
        if ($res) {

            return output(0, '修改成功');
        } else {
            return output(-1, '修改失败');
        }

    }

    /**
     *验证代理商提交数据
     * @param array $data
     * @param bool $isEmployee
     * @return array
     */
    public function checkAgent($data = [], $isEmployee = false)
    {
        if (!$isEmployee) {
            $insertData['agent_name'] = isset($data['agent_name']) ? trim($data['agent_name']) : '';
            if (empty($insertData['agent_name'])) {
                return output('-1', '请输入名称');
            }
        }
        //号码
        $insertData['phone'] = isset($data['phone']) ? trim($data['phone']) : '';
        if (empty($insertData['phone']) || !Check::checkPhone($insertData['phone'])) {
            return output('-1', '请输入正确手机号码');
        }

        //输入姓名
        $insertData['real_name'] = isset($data['real_name'])? trim($data['real_name']) : '';
        if (empty($insertData['real_name'])) {
            return output('-1', '请输入真实姓名');
        }

        $insertData['card_sn'] = isset($data['card_sn']) ? trim($data['card_sn']) : '';
        if (empty($insertData['card_sn']) || !Check::check_id_card($insertData['card_sn'])) {
            return output('-1', '请输入正确身份证号码');
        }

        //上传证件照
        $card_pic = $data['card_pic'];
        if (!empty($card_pic)) {
            $countPic = explode(",",trim($card_pic,','));
            if (count($countPic) != 3) {
                return output('-1', '请上传3张证件照（正面、反面、手持）');
            }

            $insertData['card_pic'] = '';
            foreach ($countPic as $item) {
                $item = parse_url($item);
                if (isset($item['path']) && !empty($item['path'])) {
                    $item = $item['path'];
                } else {
                    return output('-1', '文件获取失败');
                }
                $insertData['card_pic'] .= $item.',';
            }
            $insertData['card_pic'] = trim($insertData['card_pic'], ',');
        }
        if (empty($card_pic)) {
            return output('-1', '请上传证件照');
        }

        //佣金比例
        if (!$isEmployee) {
            $insertData['rate'] = isset($data['rate']) ? intval($data['rate']) : 0;
            if ($insertData['rate'] > 100 || $insertData['rate'] <= 0 ) {
                return output('-1', '比例比如是1到100之间的整数');
            }
        }
        //用户状态
        $insertData['is_use'] = isset($data['status']) ? intval($data['status']) : '';
        if (empty($insertData['is_use']) || !in_array($insertData['is_use'], array_keys($this->getStatus()))) {
            return output('-1', '请选择正确用户状态');
        }
        return ['code' => 0, 'data' => $insertData];
    }

    public function getStatus()
    {
        return config('app.agent_status');
    }


    /**
     *更新状态
     *
     */
    public function changeStatus($id = '')
    {
        $id = intval($id);
        if (empty($id)) {
            return output(-1, '参数错误');
        }
        $status = Db::name('agent')->field('is_use')->where(['id' => $id])->find();
        if (empty($status)) {
            return output(-1, '代理商不存在');
        }
        $status = $status['is_use'];
        if ($status == 1) {
            $data = ['is_use' => 2];
        } else {
            $data = ['is_use' => 1];
        }
        if (Db::name('agent')->where(['id' => $id])->update($data)) {
            return output(0, '更新状态成功');
        } else {
            return output(-1, '更新状态失败');
        }

    }







}